<--- %%NOBANNER%% --> vartype.sas
 BackForward

/*-------------------<-- Start of Description-->---------------------\
| Retrieve the column number of a variable from a dataset;           |
| Can be used to check to see if a dataset has a variable;           |
|---------------------<-- End of Description-->----------------------|
|--------------------------------------------------------------------|
|------------<-- Start of Files or Arguments Needed-->---------------|
| arguments:                                                         |
|    indata = the name of a data set you want to explore;            |
|    varname   = variable name;                                      |
|-------------<-- End of Files or Arguments Needed-->----------------|
|--------------------------------------------------------------------|
|------------------<-- Start of Files Created-->---------------------|
| Example: %vartype(one, pt); / %vartype(one, pt);                   |
| Usage:   %vartype(indata,varname);                                 |
\-------------------<-- End of Files Created-->---------------------*/
%macro vartype(indata,varname);
/*--------------------------------------------\
| Author:   Duo Zhou;                         |
| Created:  3-3-2001 11:27pm;                 |
| Modified: 8-30-2001 9:24pm;                 |
| Purpose:  Return the column of a variable in|
|           the dataset;                      |
\--------------------------------------------*/
%local _vartypevarnum_ _vartypevartype_ _vartypedsid_ _vartyperc_;
%let _vartypedsid_=%sysfunc(open(&indata));
%if &_vartypedsid_ %then %do;
   %if (%length(&varname)>0) %then %do;
      %if %chk_type(&varname)=1 %then %let _vartypevartype_=%sysfunc(vartype(&_vartypedsid_,&varname));
      %else %do;
	      %let _vartypevarnum_=%sysfunc(varnum(&_vartypedsid_,&varname)); 
	      %let _vartypevartype_=%sysfunc(vartype(&_vartypedsid_,&_vartypevarnum_)); 
      %end;
      %if (%index(%quote(&_vartypevartype_), %quote(C))) %then 2;
      %else %if (%index(%quote(&_vartypevartype_), %quote(N))) %then 1; 
      %else 0;
   %end;
   %let _vartyperc_=%sysfunc(close(&_vartypedsid_));
%end;
%else %do;
   %put ==> Alert! Open for data set "&indata" failed.; 0
%end; 
%mend vartype;